package com.example.terminalservice.controller;

import com.example.terminalservice.common.Result;
import com.example.terminalservice.pojo.Session;
import com.example.terminalservice.pojo.dto.CommandDTO;
import com.example.terminalservice.pojo.dto.SessionDTO;
import com.example.terminalservice.service.SessionService;
import com.example.terminalservice.utils.SSHUtils;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author 28142
 * @description SessionController
 * @date 2023/8/7 11:35
 */
@RestController
@RequestMapping("/session")
public class SessionController {
    @Autowired
    private SessionService sessionService;

    /**
     * 分页条件查询会话列表
     * @author 杨雨盛
     * @date 2023/8/11 15:26
     * @param pageNum
     * @param pageSize
     * @param name
     * @param host
     * @return com.example.terminalservice.common.Result
     */
    @GetMapping("/{pageNum}/{pageSize}")
    @PreAuthorize("hasAnyAuthority('sys:session')")
    public Result findPage(@PathVariable Integer pageNum,
                           @PathVariable Integer pageSize,
                           @RequestParam(required = false) String name,
                           @RequestParam(required = false) String host) {
        return sessionService.findPage(pageNum, pageSize, name, host);
    }

    /**
     * 添加或修改会话信息
     * @author 杨雨盛
     * @date 2023/8/11 15:29
     * @param session
     * @return com.example.terminalservice.common.Result
     */
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:session:add', 'sys:session:update')")
    public Result saveSession(@RequestBody Session session) {
        return sessionService.saveOrUpdate(session) ? Result.success() : Result.error();
    }

    /**
     * 根据id删除会话
     * @author 杨雨盛
     * @date 2023/8/11 15:30
     * @param id
     * @return com.example.terminalservice.common.Result
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys:session:del')")
    public Result delSession(@PathVariable Integer id) {
        return sessionService.removeById(id) ? Result.success() : Result.error();
    }
}
